import { combineReducers } from "redux";
import * as types from "./actionTypes02";

const reducerBool = (state = false, action) => {
    switch (action.type) {
        case types.SUCCESS:
        case types.FAIL:
            return action.data;
        default:
            return state;
    }
};

// 任务 增加/设置完成/删除/清除所有
const taskListReducer = (state = [], action) => {
    switch (action.type) {
        case types.GET_TASK_LIST:
            return action.data;
        case types.CREATE_TASK:
            return [...action.data, ...state];
        case types.SET_TASK_DONE:
            return action.data;
        case types.DEL_ONE_TASK:
            const list = [...state];
            const taskId = action.data[0].taskId;
            return list.filter((item) => item.taskId !== taskId);
        case types.CLEAR_ALL_TASKS:
            return [];
        default:
            return state;
    }
};

// 任务指示牌
const notDoneDataReducer = (state = [], action) => {
    switch (action.type) {
        case types.NOT_DONE_DATA:
            return action.data;
        default:
            return state;
    }
};

// 自定义节日 获取/增加/删除/清除所有/重新编辑
const festivalListReducer = (state = [], action) => {
    switch (action.type) {
        case types.GET_FESTIVAL_LIST:
            return action.data;
        case types.ADD_ONE_FESTIVAL:
            return [...state, action.data];
        case types.REMOVE_ONE_FESTIVAL:
            const data = [...state];
            return data.filter((item) => item.createTime !== action.data);
        case types.CLEAR_ALL_FESTIVAL:
            return action.data;
        case types.REEDIT_FESTIVAL:
            const { reData, createTime } = action.data;
            const stateData = [...state];
            return [
                ...stateData.filter((item) => item.createTime !== createTime),
                reData,
            ];
        default:
            return state;
    }
};

// 获取某个自定义节日
const getOneFestivalReducer = (state = {}, action) => {
    switch (action.type) {
        case types.GET_ONE_FESTIVAL:
            return action.data;
        default:
            return state;
    }
};

// 每月未完成数据
const notDoneMonthReducer = (state = {}, action) => {
    switch (action.type) {
        case types.MONTH_ONT_DONE:
            return action.data;
        default:
            return state;
    }
};

// 获取往期功能 点赞 取消赞
const previousReducer = (state = [], action) => {
    switch (action.type) {
        case types.GET_PREVIOUS:
            return action.data;
        case types.ADD_PREVIOUS_LIKE:
        case types.CUT_PREVIOUS_LIKE:
            const r0 = [...state];
            const r1 = r0.map((item) =>
                item.key === action.data.key ? action.data : item
            );
            return r1;
        default:
            return state;
    }
};

// 研发项目操作
const devItemReducer = (state = [], action) => {
    switch (action.type) {
        case types.GET_DEV_ITEM_LIST:
            return action.data;
        default:
            return state;
    }
};

// 留言榜数据
const leaveMessageRankReducer = (state = {}, action) => {
    switch (action.type) {
        case types.GET_LEAVE_MESSAGE_RANK:
            return action.data;
        default:
            return state;
    }
};

// 我的留言
const myLeaveMessageReducer = (state = [], action) => {
    switch (action.type) {
        case types.GET_MY_LEAVE_MESSAGE:
            return action.data;
        default:
            return state;
    }
};

// 所有留言
const allLeaveMessagesReducer = (state = [], action) => {
    switch (action.type) {
        case types.GET_ALL_LEAVE_MESSAGE:
            return action.data;
        default:
            return state;
    }
};

// 所有被采纳的留言
const allAcceptLeaveMessageReducer = (state = [], action) => {
    switch (action.type) {
        case types.GET_ALL_ACCEPT_LEAVE_MESSAGE:
            return action.data;
        default:
            return state;
    }
};

// 某个留言
const oneLeaveMessageReducer = (state = {}, action) => {
    switch (action.type) {
        case types.GET_ONE_LEAVE_MESSAGE:
            return action.data;
        case types.ADD_LIKE_LEAVE_MESSAGE:
            return action.data;
        case types.CUT_LIKE_LEAVE_MESSAGE:
            return action.data;
        default:
            return state;
    }
};

// 控制chat的显示开关
const isSearchReducer = (state = false, action) => {
    switch (action.type) {
        case types.IS_IN_SEARCH:
            return action.data;
        default:
            return state;
    }
};

// 添加好友时输入的搜索内容
const addSearchValReducer = (state = "", action) => {
    switch (action.type) {
        case types.ADD_SEARCH_VALUE:
            return action.data;
        default:
            return state;
    }
};

// 在右侧聊天栏头部显示的当前选择项的名称
const selectItemNameShowChatHeadReducer = (state = "", action) => {
    switch (action.type) {
        case types.SELECT_ITEM_NAME_SHOW_CHAT_HEAD:
            return action.data;
        default:
            return state || null;
    }
};

// 选择的表情包信息
const emojiInfoReducer = (state = {}, action) => {
    switch (action.type) {
        case types.EMOJI_SELECT_INFO:
            return action.data;
        default:
            return state;
    }
};

// 记录编辑区光标位置
const currentRangeReducer = (state = null, action) => {
    switch (action.type) {
        case types.RECORD_CURRENT_RANGE:
            return action.data;
        default:
            return state;
    }
};

// 申请好友列表
const applyFriendsListReducer = (state = [], action) => {
    switch (action.type) {
        case types.APPLY_FRIENDS_LIST:
            return action.data;
        default:
            return state;
    }
};

// 申请添加好友
const applyAddFriendReducer = (state = false, action) => {
    switch (action.type) {
        case types.APPLY_ADD_FRIEND:
            return action.data;
        default:
            return state;
    }
};

// 是否打开了添加好友界面
const isShowAddFriendBouncedReducer = (state = false, action) => {
    switch (action.type) {
        case types.IS_SHOW_ADD_FRIEND_BOUNCED:
            return action.data;
        default:
            return state;
    }
};

// 好友列表
const frinedsListReducer = (state = [], action) => {
    switch (action.type) {
        case types.FRIENDS_LIST:
            return action.data;
        case types.ADD_SELF_FRINED:
            return [action.data, ...state];
        case types.ADD_FRIEND:
            return [action.data, ...state];
        case types.UPDATE_LAST_CHAT_MESSAGE:
            const { chater, lastChatMessage } = action.data;
            return state.map((item) => {
                if (item.userName === chater) {
                    return {
                        ...item,
                        lastMessage: lastChatMessage,
                    };
                } else {
                    return item;
                }
            });
        case types.SET_CHAT_MESSAGE_READ:
            return action.data;
        case types.MOTIFY_CHATER_BYNAME:
            return action.data;
        case types.SET_IS_BLOCKED_CHAT:
            return state.map((item) => {
                if (item.userName === action.data.userName) {
                    return {
                        ...item,
                        isBlockedChat: action.data.isBlockedChat || false,
                    };
                } else {
                    return item;
                }
            });
        default:
            return state;
    }
};

// 和某个用户的聊天消息列表
const chatMessageListReducer = (state = [], action) => {
    switch (action.type) {
        case types.GET_CHAT_MESSAGE_LIST:
            return action.data;
        case types.UPDATE_NEW_MESSAGE:
            return [...state, ...action.data];
        case types.DELETE_THE_CHAT_MESSAGE:
            return action.data;
        default:
            return state;
    }
};

// friend item 给friend main同步传递数据（子传父）
const friendItemToMainDataReducer = (state = {}, action) => {
    switch (action.type) {
        case types.FRIEND_ITEM_TO_MAIN_DATA:
            return action.data;
        default:
            return state;
    }
};

// 是否显示修改备注名称的卡片
const isShowMotifyBynameCardReducer = (state = false, action) => {
    switch (action.type) {
        case types.IS_SHOW_MOTIFY_BYNAME_CARD:
            return action.data;
        default:
            return state;
    }
};

const reducers = combineReducers({
    reducerBool,
    taskListReducer,
    notDoneDataReducer,
    notDoneMonthReducer,
    festivalListReducer,
    getOneFestivalReducer,
    previousReducer,
    devItemReducer,
    leaveMessageRankReducer,
    myLeaveMessageReducer,
    allLeaveMessagesReducer,
    allAcceptLeaveMessageReducer,
    oneLeaveMessageReducer,
    isSearchReducer,
    addSearchValReducer,
    selectItemNameShowChatHeadReducer,
    emojiInfoReducer,
    currentRangeReducer,
    applyFriendsListReducer,
    applyAddFriendReducer,
    isShowAddFriendBouncedReducer,
    frinedsListReducer,
    chatMessageListReducer,
    friendItemToMainDataReducer,
    isShowMotifyBynameCardReducer,
});

export default reducers;
